package com.cloud.message.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.constants.IMConstants;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.ImOpenType;
import com.cloud.exception.CustomException;
import com.cloud.message.entity.SysImGroup;
import com.cloud.message.entity.SysImGroupUser;
import com.cloud.message.mapper.SysImGroupMapper;
import com.cloud.message.mapper.SysImGroupUserMapper;
import com.cloud.message.service.ISysImGroupService;
import com.cloud.service.RedisService;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * IM消息群组
 *
 * @author yzj
 * @since 2024-01-09
 */
@Service
public class SysImGroupServiceImpl extends ServiceImpl<SysImGroupMapper, SysImGroup> implements ISysImGroupService {

    @Resource
    private SysImGroupMapper sysImGroupMapper;

    @Resource
    private SysImGroupUserMapper sysImGroupUserMapper;

    @Resource
    private RedisService redisService;

    /**
     * IM消息群组列表
     */
    @Override
    public List<SysImGroup> getSysImGroupList(SysImGroup imGroup) {
        return sysImGroupMapper.getSysImGroupList(imGroup);
    }

    /**
     * IM消息群组详情
     */
    @Override
    public SysImGroup getSysImGroupById(Long id) {
        return sysImGroupMapper.selectById(id);
    }

    /**
     * IM消息群组新增
     */
    @Override
    public Result saveSysImGroup(SysImGroup imGroup) {
        if (StringUtils.isEmpty(imGroup.getGroupName())) {
            throw new CustomException("创建群名称不能为空");
        }
        imGroup.setMaster(imGroup.getUserId());
        sysImGroupMapper.insert(imGroup);
        // 添加群组用户关联信息
        SysImGroupUser imGroupUser = new SysImGroupUser();
        imGroupUser.setGroupId(imGroup.getId());
        imGroupUser.setUserId(imGroup.getUserId());
        sysImGroupUserMapper.insert(imGroupUser);
        return Result.success("操作成功");
    }

    /**
     * IM消息群组修改
     */
    @Override
    public Result editSysImGroup(SysImGroup imGroup) {
        if (StringUtils.isEmpty(imGroup.getGroupName())) {
            throw new CustomException("创建群名称不能为空");
        }
        imGroup.setMaster(imGroup.getUserId());
        sysImGroupMapper.updateById(imGroup);
        return Result.success("操作成功");
    }

    /**
     * IM消息群组删除
     */
    @Override
    public int removeIds(List<Long> ids) {
        // 批量删除群组用户关联信息
        sysImGroupUserMapper.batchGroupUser(ids);
        // 批量删除群组信息
        return sysImGroupMapper.deleteBatchIds(ids);
    }

    /**
     * 群组新增用户信息
     */
    @Override
    public Result saveGroupUser(SysImGroupUser imGroupUser) {
        SysImGroup imGroup = getSysImGroupById(imGroupUser.getGroupId());
        if (StringUtils.isNull(imGroup)) {
            return Result.message("操作失败:群组信息不存在");
        }
        if (ImOpenType.ALLOW.getCode().equals(imGroup.getOpenInvite())) {
            return Result.message("操作失败:群组不允许他人邀请");
        }
        sysImGroupUserMapper.insert(imGroupUser);
        return Result.success("操作成功");
    }

    /**
     * 群组删除用户信息
     */
    @Override
    public Result delGroupUser(SysImGroupUser imGroupUser) {
        SysImGroup imGroup = getSysImGroupById(imGroupUser.getGroupId());
        if (StringUtils.isNull(imGroup)) {
            return Result.message("操作失败:群组信息不存在");
        }
        sysImGroupUserMapper.delGroupUser(imGroupUser);
        return Result.success("操作成功");
    }

    /**
     * IM消息群组用户列表
     */
    @Override
    public List<SysImGroupUser> getImGroupUserList(Long groupId) {
        String groupKey = String.valueOf(groupId);
        List<SysImGroupUser> groupUserList = redisService.getHashKey(IMConstants.imGroupUser, groupKey);
        if (StringUtils.isNull(groupUserList)) {
            // 从数据库获取群组用户列表
            groupUserList = sysImGroupUserMapper.getImGroupUserList(groupId);
            redisService.setHash(IMConstants.imGroupUser, groupKey, groupUserList);
        }
        return groupUserList;
    }

}
